package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.L1Mon;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1MonTable {
  private static final Log _log = LogFactory.getLog(L1MonTable.class);
  
  private static final Map<Integer, L1Mon> _mons = new HashMap<>();
  
  private static L1MonTable _instance;
  
  public static L1MonTable get() {
    if (_instance == null)
      _instance = new L1MonTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `w_怪物击杀系统`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int Npc_Mid = rs.getInt("Npc编号");
        int Dorp_Id = rs.getInt("共同攻击奖励");
        int Dorp_Lost_Id = rs.getInt("尾刀攻击者奖励");
        int x = rs.getInt("共同攻击者传送X轴");
        int y = rs.getInt("共同攻击者传送Y轴");
        int map = rs.getInt("共同攻击者传送地图编号");
        int x2 = rs.getInt("尾刀攻击者传送X轴");
        int y2 = rs.getInt("尾刀攻击者传送Y轴");
        int map2 = rs.getInt("尾刀攻击者传送地图编号");
        int pcdmg = rs.getInt("打击怪物总伤害最低值");
        L1Mon Mon = new L1Mon();
        Mon.setNpcmid(Npc_Mid);
        Mon.setDorpid(Dorp_Id);
        Mon.setDorplostid(Dorp_Lost_Id);
        Mon.setX(x);
        Mon.setY(y);
        Mon.setMap(map);
        Mon.setX1(x2);
        Mon.setY1(y2);
        Mon.setMap1(map2);
        Mon.setpcdmg(pcdmg);
        _mons.put(Integer.valueOf(Npc_Mid), Mon);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("读取->w_怪物击杀系统资料: " + _mons.size() + "(" + timer.get() + "ms)");
  }
  
  public L1Mon getMon(int Npc_Mid) {
    return _mons.get(Integer.valueOf(Npc_Mid));
  }
}
